<?php
namespace Admin\Controller;
use Think\Controller;
class WordsController extends Controller {
    public function wordslist(){
    	//调用模型层获取数据
        $wordsmodel=M('words');
        //var_dump($usermodel);
        $words=$wordsmodel->select();
    	//调用视图层显示数据
    	$this->words=$words;
    	$this->display();
    }

     public function addwords(){
        $this->display();
    }

    public function doaddwords(){
        $data=$_POST;
        $file=$_FILES;
        //dump($file['photo']['size']);
        
        //没有头像
        $wordsmodel = M('words'); // 实例化User模型
        $createresult=$wordsmodel->create();   // 根据表单提交的POST数据创建数据对象
          if ($createresult) {
         
             if ($file['w_pic']['size']==0) {//没有上传轮播图片
                        $wordsmodel->w_pic= "nopic.jpg"; // 用户不上传头像默认nopic.jpg
                    }else{//有图片
                            $info=$this->addupload();
                            $wordsmodel->w_pic = $info['w_pic']['savename']; // 保存上传的照片根据需要自行组装
                          }
                  
              
                 
                 $addresult=$wordsmodel->add(); // 写入用户数据到数据库
                     if (false !== $addresult)
                             {
                            $this->success('上传成功！', '../Words/wordslist');
                             }else{ 
                                     $this->error('数据错误'); 
                                    }
                                   }else{   $this->error($wordsmodel->getError());
                                   }  
         

    
    }
     public function addupload(){
                //上传头像
              $upload = new \Think\Upload();// 实例化上传类
              $upload->maxSize  = 3145728 ;// 设置附件上传大小
              $upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
              $upload->rootPath  =  './';
              $upload->savePath =  'Public/Admin/vmphoto/';// 设置附件上传目录
              $upload->autoSub = false;
              $upload->saveName = date("YmdHis").rand(100,999);//定义名字
               // 上传单个文件 
              $info = $upload->upload();                    
                  if(!$info) {// 上传错误提示错误信息
                      $this->error($upload->getError());
                    }else{// 上传成功 获取上传文件信息
                return $info;
                         }
    }

  public function editwords(){
        $w_id=$_GET['w_id'];
        $wordsModel=M('words');//表明,conf中定义前缀
        $words = $wordsModel->where("w_id=$w_id")->select();
        $this->words=$words;//name属性对应的值 $this->对应值=$find;
        $this->display();
    }
    
    public function updatewords(){

          $data=$_POST;
            $file=$_FILES;
            
            $w_id=$data['w_id'];
                   
            $wordsModel = M('words'); // 实例化User模型
            // 要修改的数据对象属性赋值
         //  $result=$activityModel->where("act_id=$act_id")->save($data);
        
          if ($file['w_pic']['size']==0) {
            //没有修改头像
            $result=$wordsModel->where("w_id=$w_id")->save($data);
            
         
  
        }else{
            //修改头像
            $upload = new \Think\Upload();// 实例化上传类
            $upload->maxSize  = 3145728 ;// 设置附件上传大小
            $upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
            $upload->rootPath  =  './';
            $upload->savePath =  'Public/Admin/vmphoto/';// 设置附件上传目录
            $upload->autoSub = false;
            $upload->saveName = date("YmdHis").rand(100,999);//定义名字
            $info = $upload->upload();
    
            if(!$info) {// 上传错误提示错误信息
                    $this->error($upload->getError());
             }else{// 上传成功 获取上传文件信息
                 //保存表单数据 包括附件数据
                


                $data['w_pic']= $info['w_pic']['savename']; 
                $result=$wordsModel->where("w_id=$w_id")->save($data);
                    if ($data['oldpic']!='nopic.jpg') {
                    $oldpicPath='Public/Admin/vmphoto/'.$data['oldpic'];
                    unlink($oldpicPath);
                        }
                    }
            }
           
           if($result){
                $this->success('修改成功！','../Words/wordslist');
                }
               else{
                   $this->error('修改失败！');
                  //$this->success('修改成功！','../Activity/activitylist');
                   }

    }



   public function delwords($w_id){
   
    $wordsModel = M("words"); // 实例化User对象
    $words=$wordsModel->where("w_id=$w_id")->select();
    $result=$wordsModel->where("w_id=$w_id")->delete(); 
        if($result){                    
            $this->success('删除成功');//设置成功后跳转页面的地址，默认的返回页面是$_SERVER['HTTP_REFERER']
            } else {
             $this->error('删除失败'); //错误页面的默认跳转页面是返回前一页，通常不需要设置
            }

    }

}
    